package org.example.partnerbacked.controller;

<<<<<<< HEAD
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
=======
<<<<<<< HEAD
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
=======
>>>>>>> 97101e507a3cd6e1c395aedffbc4a5eab5000716
>>>>>>> 515eae65c6ba5dc43fc118901c72b817d3b49dba
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.example.partnerbacked.common.BaseResponse;
import org.example.partnerbacked.common.ErrorCode;
import org.example.partnerbacked.common.ResultUtils;
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> 515eae65c6ba5dc43fc118901c72b817d3b49dba
import org.example.partnerbacked.config.ConfigSession;
import org.example.partnerbacked.exception.BusinessException;
import org.example.partnerbacked.model.User;
import org.example.partnerbacked.model.requste.UserLoginRequest;
import org.example.partnerbacked.model.requste.UserRegisterRequest;
import org.example.partnerbacked.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static org.example.partnerbacked.common.ResultUtils.success;

<<<<<<< HEAD
=======
=======
import org.example.partnerbacked.model.User;
import org.example.partnerbacked.model.requste.UserLoginRequest;
import org.example.partnerbacked.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.web.bind.annotation.*;

>>>>>>> 97101e507a3cd6e1c395aedffbc4a5eab5000716
>>>>>>> 515eae65c6ba5dc43fc118901c72b817d3b49dba
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;
     @Resource
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> 515eae65c6ba5dc43fc118901c72b817d3b49dba
    public RedisTemplate<String ,Object> redisTemplate;
    @GetMapping("test")
    public String test() {

<<<<<<< HEAD
=======
=======
    public RedisTemplate redisTemplate;
    @GetMapping("test")
    public String test() {
>>>>>>> 97101e507a3cd6e1c395aedffbc4a5eab5000716
>>>>>>> 515eae65c6ba5dc43fc118901c72b817d3b49dba
      log.info("test"+redisTemplate);

        return "test";
    }
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> 515eae65c6ba5dc43fc118901c72b817d3b49dba
     @PostMapping("register")
     public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
            if(userRegisterRequest == null){
               throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
            String userAccount = userRegisterRequest.getUserAccount();
            String userPassword = userRegisterRequest.getUserPassword();
            String checkPassword = userRegisterRequest.getCheckPassword();
            String planetCode = userRegisterRequest.getPlanetCode();
            if(StringUtils.isAnyBlank(userAccount, userPassword, checkPassword, planetCode)){
                return ResultUtils.error(ErrorCode.PARAMS_ERROR);
            }
            //userId小于0，表示插入失败
            long userId = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);
            return ResultUtils.success(userId);
    }
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw  new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
<<<<<<< HEAD
=======
=======

    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
>>>>>>> 97101e507a3cd6e1c395aedffbc4a5eab5000716
>>>>>>> 515eae65c6ba5dc43fc118901c72b817d3b49dba
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.userLogin(userAccount, userPassword, request);
<<<<<<< HEAD
        return ResultUtils.success(user);
=======
<<<<<<< HEAD
        return success(user);
>>>>>>> 515eae65c6ba5dc43fc118901c72b817d3b49dba
    }
    @PostMapping("/logout")

    public BaseResponse<Integer> userLogout(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(ConfigSession.USER_LOGIN_STATE);
        if (user == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "未登录");
        }
        int result = userService.userLogout(request);
        return ResultUtils.success(result);
    }
    @GetMapping("/current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        Object user = request.getSession().getAttribute(ConfigSession.USER_LOGIN_STATE);
        User currentUser = (User) user;
        if (currentUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        long userId = currentUser.getId();
        //todo 校验用户是否合法
        User userByMySql = userService.getById(userId);
        User safetyUser = userService.getSafetyUser(userByMySql);
        return ResultUtils.success(safetyUser);
    }
    @GetMapping("/search")
    public BaseResponse<List<User>> searchUser(String username, HttpServletRequest request)  {
        if (!userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(username)) {
            queryWrapper.like("username", username);
        }
        List<User> userList = userService.list(queryWrapper);
        List<User> list = userList.stream().map(user -> userService.getSafetyUser(user)).collect(Collectors.toList());
        return ResultUtils.success(list);
    }
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody long id, HttpServletRequest request) {
        if (!userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = userService.removeById(id);
        return ResultUtils.success(b);
    }
    @GetMapping("/search/tags")
    public BaseResponse<List<User>> searchUsersByTags(@RequestParam(required = false) List<String> tagNameList) {
        if (CollectionUtils.isEmpty(tagNameList)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<User> userList = userService.searchUsersByTags(tagNameList);
        return ResultUtils.success(userList);
    }
    @PostMapping("/update")
    public BaseResponse<Integer> updateUser(@RequestBody User user, HttpServletRequest request) {

          int result = userService.updateUser(user, request);
          return ResultUtils.success(result);
    }

    @GetMapping("/match")
    public BaseResponse<List<User>> matchUsers(long num, HttpServletRequest request) {
        return ResultUtils.success(userService.matchUsers(num, request));
<<<<<<< HEAD
    }
    @GetMapping("/recommend")
    public BaseResponse<Page<User>> recommendUsers(long pageSize, long pageNum, HttpServletRequest request) {
        return ResultUtils.success(userService.recommendUsers(pageSize, pageNum, request));
=======
=======
        return ResultUtils.success(user);
>>>>>>> 97101e507a3cd6e1c395aedffbc4a5eab5000716
>>>>>>> 515eae65c6ba5dc43fc118901c72b817d3b49dba
    }
}
